package com.mk.admin.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.mk.admin.mapper.CampusMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.entity.CampusEntity;
import com.mk.admin.model.query.CampusCodeQuery;
import com.mk.admin.model.query.CampusQuery;
import com.mk.admin.model.query.CampusRemoteQuery;
import com.mk.admin.model.query.CampusUserQuery;
import com.mk.admin.model.vo.CampusVo;
import com.mk.admin.service.CampusService;
import com.mk.admin.util.HttpTool;
import com.mk.admin.util.JSONCopy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CampusServiceImpl implements CampusService {
    @Autowired
    private CampusMapper campusMapper;

    @Override
    public ResultDto select(CampusQuery query) {
        CampusEntity entity = JSONCopy.copy(query, CampusEntity.class);
        return ResultDto.data(campusMapper.count(entity), campusMapper.select(entity));
    }

    @Override
    public ResultDto fresh(CampusRemoteQuery query) {
        JSONObject jsonObject = HttpTool.postJson("com_0029", query);
        Object result = jsonObject.get("result");
        if (result != null && !"[]".equals(result)) {
            return ResultDto.data(result);
        }
        return ResultDto.data(jsonObject);
    }

    @Override
    public ResultDto campusCode(CampusCodeQuery query) {
        JSONObject jsonObject = HttpTool.postJson("com_0034", query);
        Object result = jsonObject.get("result");
        if (result != null && !"[]".equals(result)) {
            return ResultDto.data(result);
        }
        return ResultDto.data(jsonObject);
    }

    @Override
    public ResultDto campusUser(CampusUserQuery query) {
        JSONObject jsonObject = HttpTool.postJson("com_0030", query);
        Object result = jsonObject.get("result");
        if (result != null && !"[]".equals(result)) {
            return ResultDto.data(result);
        }
        return ResultDto.data(jsonObject);
    }

    @Override
    public ResultDto add(CampusVo vo) {
        return ResultDto.data(campusMapper.insert(JSONCopy.copy(vo, CampusEntity.class)));
    }

    @Override
    public ResultDto update(CampusVo vo) {
        return ResultDto.data(campusMapper.updateById(JSONCopy.copy(vo, CampusEntity.class)));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(campusMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(campusMapper.status(id));
    }
}
